//
//  HotCollectionReusableView.m
//  Inner  Voice
//
//  Created by Martin.Wang on 16/3/3.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "HotCollectionReusableView.h"

#import "AFNetworking.h"

@interface HotCollectionReusableView ()<UIScrollViewDelegate>{
    
    CGRect zongViewFrame;
}

@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, strong)NSMutableArray *imageViewArray;

@property (nonatomic, strong)NSArray *bannerData;

@property (nonatomic, assign)int page;

@property (nonatomic, strong)NSDictionary *sound;

@property (nonatomic, strong)NSMutableArray *soundsArray;

@end

#define ImageCount 3

@implementation HotCollectionReusableView
- (NSMutableArray*)imageViewArray{
    if (_imageViewArray == nil) {
        _imageViewArray = [NSMutableArray array];
    }
    return _imageViewArray;
}

- (NSMutableArray*)soundsArray{
    if (_soundsArray == nil) {
        _soundsArray = [NSMutableArray array];
    }
    return _soundsArray;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.topView.frame = self.frame;
    zongViewFrame = self.frame;
    
    if (self.topView.subviews.count == 1) {
        [self makeScrollView];
    }
    
    
    
}

- (void)bannerSounds:(NSString*)soundid{
    
    NSString *urlStr = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/Home/Banner/backsoundRequest?soundid=%@",soundid];
    
    AFHTTPSessionManager *httpSession = [AFHTTPSessionManager manager];
    
    [httpSession GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        
        [self.soundsArray addObject:dic[@"data"][0]];
        
        if (self.soundsArray.count == ImageCount) {
            
            for (int a = 0; a < ImageCount; a++) {
                
                UIImageView *view = self.imageViewArray[a];
                
                view.userInteractionEnabled = YES;
                
            }
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"推荐 ERROR%@",error);
    }];
    
    
    
}


- (void)bannerRreshen{
    
    AFHTTPSessionManager *httpSession = [AFHTTPSessionManager manager];
    [httpSession POST:@"http://1.wangtest11.applinzi.com/Home/Banner/backhomeHotBanner" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        
        self.bannerData = dic[@"data"];
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        for (int a = 0; a < ImageCount; a++) {
            
            dispatch_async(queue, ^{
                NSString *str = self.bannerData[a][@"pic"];
                NSURL *Url = [NSURL URLWithString:str];
                NSData *data = [NSData dataWithContentsOfURL:Url];
                UIImage *image = [UIImage imageWithData:data];
                [self updateImage:image withImageView:self.imageViewArray[a]];
            });
            
        }
        
        
        for (int a = 0; a < ImageCount; a++) {
            
            NSDictionary *dic = self.bannerData[a];
            
            [self bannerSounds:dic[@"url"]];
            
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"推荐 ERROR %@",error);
    }];
    
    
}

- (void)updateImage:(UIImage*)image withImageView:(UIImageView*)imageView{
    
    dispatch_sync(dispatch_get_main_queue(), ^{
        imageView.image = image;
    });
}

- (void)tapImageView{
    
    self.sound = self.soundsArray[self.pageControl.currentPage];
    
    
    self.push(self.sound);
    
}

- (void)makeScrollView{
    
    //取出myScrollView的宽高
    CGFloat scrollVieW = zongViewFrame.size.width - 10;
    
    CGFloat scrollVieH = zongViewFrame.size.height-30;
    
    NSLog(@"推荐 %f",self.frame.size.width);
    
    //手动生成imageView并加入myScrollView，生成的imageView是向右延伸的
    for (int a = 1; a <= ImageCount; a++) {
        
        CGFloat imageViewX = (a - 1) * scrollVieW;
        
        CGFloat iamgeViewY = 0;
        
        CGFloat imageViewW = scrollVieW;
        
        CGFloat imageViewH = scrollVieH;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageViewX, iamgeViewY, imageViewW, imageViewH)];
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
        
        [imageView addGestureRecognizer:singleTap];
        
        NSString *imageName = [NSString stringWithFormat:@"%d",a];
        
        imageView.image = [UIImage imageNamed:imageName];
        
        [self.imageViewArray addObject:imageView];
        
        [self.myScrollView addSubview:imageView];
    }
    
    self.myScrollView.delegate = self;
    //myScrollView显示画布大小的设置
    self.myScrollView.contentSize = CGSizeMake(scrollVieW * ImageCount, scrollVieH);
    //分页显示！
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
//     隐藏进度条
    self.myScrollView.showsVerticalScrollIndicator = FALSE;
    self.myScrollView.showsHorizontalScrollIndicator = FALSE;
    //演示点的创建
    UIPageControl *pageContro = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    //创建一个点为myScrollView的中心点
    CGPoint tempPoint = self.center;
    //tempPoint向下移动70
    tempPoint.y += self.frame.size.height/4;
    //演示点的中心点为tempPoint
    pageContro.center = tempPoint;
    //演示点未被选中的颜色
    pageContro.pageIndicatorTintColor = [UIColor grayColor];
    //演示点被选中的颜色
    pageContro.currentPageIndicatorTintColor = [UIColor whiteColor];
    //演示点的个数
    pageContro.numberOfPages = ImageCount;
    
    pageContro.currentPage = 0;
//    pageContro.backgroundColor = [UIColor blackColor];
    
    //自身属性的pageControl为pageContro
    self.pageControl = pageContro;
    //将pageContro加到View
    [self.topView addSubview:pageContro];
    //设置时间循环播放
    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(cycleImage) userInfo:nil repeats:YES];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.frame.size.width;
    self.pageControl.currentPage = (contentOffSetX + scrollViewW * 0.5) / scrollViewW ;
}


//循环播放方法
- (void)cycleImage {
    CGFloat moveX = 0;
    if (self.pageControl.currentPage != 2) {
        moveX = (self.pageControl.currentPage + 1) * self.myScrollView.frame.size.width;
    }else {
        self.pageControl.currentPage = 0;
    }
    CGPoint tempPoint = CGPointMake(moveX, 0);
    [self.myScrollView setContentOffset:tempPoint animated:YES];
}

- (void)awakeFromNib {
    
    [self bannerRreshen];
    
    
    
    //    [self makeScrollView];
    
}


@end
